import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Layout from '../components/Layout'
import User from '../components/User'
import Type from "../components/Type";
import Login from "../components/Login";

Vue.use(VueRouter)
import axios from 'axios'
import Error from "../components/Error";

const routes = [
    // {
    //   path: '/',
    //   name: 'Home',
    //   component: Home
    // },
    {
        path: '/error',
        name: 'Error',
        component: Error
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        name: 'Layout',
        component: Layout

        , redirect: '/user'

        , children: [
            {
                path: 'user',
                name: 'User',
                component: User,
                meta: {requiresAuth: true}

            },
            {
                path: 'type',
                name: 'Type',
                component: Type,
                meta: {requiresAuth: true}
            }
        ]
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
let _this = this

router.beforeEach((to, from, next) => {
    console.log(to.meta)
    console.log(localStorage.getItem('token'))
    if (to.meta.requiresAuth) {
        if (localStorage.getItem('token')) {
            // axios.get('http://localhost:9090/checktoekn?token=' + localStorage.getItem('token')).then(res => {
            //     if (res.data.code === '0') {
            //         next()
            //     } else {
            //         next('/login')
            //
            //     }
            // })
            next()

        } else {
            next('/login')
        }
    } else {
        next()

    }

    // if (to.meta.requi)
})
export default router
